<?php
namespace serv\queue\warehouse;

use L;
use of;
use Exception;
use common\commonUpdateTrait;
use common\batchInsert;
use of\base\com\mq;
use Sdk\Fourstar\callFourStarApiUtil;
use traits\ProductTrait;
use traits\StockTrait;


class fourstar
{
    //同步开仓库了的库存
    public static function pull($arr)
    {
        try {
            $arr = $arr['data'];
            $action = 'GetInventoryList';
            $data = [
                'StockID' => $arr['warehouse_code'],
            ];
            //查询库存信息
            $result = (new callFourStarApiUtil())->callApi($action, $data);
            if (!empty($result)) {
                $stockArr = $result;
                foreach ($stockArr as $k => $val) {
                    $key = "check_fourstar_sku_stock_{$arr['warehouse_id']}_{$val['SKU']}";
                    $val = array_merge($val, ['warehouse_id' => $arr['warehouse_id']]);
                    //echo json_encode($val, JSON_UNESCAPED_UNICODE); exit;
                    mq::set(["checkFourstarStock", $key, $k], $val);
                }
            }
            return true;
        } catch (Exception $exp) {
            return false;
        }
    }

    //更新库存或者写入新的库存数据
    public static function check($arr)
    {
        set_time_limit(0);
        try {
            of::work(['warehouse']);
            $val = $arr['data'];
            $sql = "SELECT stock_id, sku FROM stock WHERE 1 
                    AND warehouse_id = '{$val['warehouse_id']}' 
                    AND sku_code = '{$val['SKU']}';";
            $result = L::sql($sql, 'warehouse');
            if (!empty($result)) {
                //更新数据
                $data = [
                    'sku' => StockTrait::tran2sku($val['SKU']),
                    'available_stock' => $val['AvailableQty'], //可用
                    'lock_stock' => $val['UsedQty'], //锁
                    'stock' => $val['StockQty'], //在库
                    'belong' => of::config('env.PUBLIC_BELONG'),
                    'status' => StockTrait::stockStatus($val['SKU']),
                    'updated_by' => of::config('env.SYSTEM_ID'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                //更新
                commonUpdateTrait::update('stock', $data, $result[0]['stock_id'], 'stock_id');
            } else {
                //插入数据
                $position = self::position($val['warehouse_id']);
                $sku = StockTrait::tran2sku($val['SKU']);
                $skuInfo = ProductTrait::skuInfo($sku);
                $data[] = [
                    'position_id' => !empty($position) ? $position['position_id'] : 1,
                    'area_id' => !empty($position) ? $position['area_id'] : 1,
                    'warehouse_id' => $val['warehouse_id'],
                    'sku' => $sku,
                    'sku_code' => $val['SKU'],
                    'sku_name' => !empty($skuInfo) ? $skuInfo['sku_name'] : '',
                    'sku_name_en' => !empty($skuInfo) ? $skuInfo['sku_name_en'] : '',
                    'stock' => $val['StockQty'],
                    'available_stock' => $val['AvailableQty'],
                    'lock_stock' => $val['UsedQty'],
                    'on_way_stock' =>  0, //同步不了在途
                    'belong' => of::config('env.PUBLIC_BELONG'),
                    'status' => StockTrait::stockStatus($val['SKU']),
                    'created_by' => of::config('env.SYSTEM_ID'),
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_by' => of::config('env.SYSTEM_ID'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                $sql = batchInsert::insert('stock', $data);
                L::sql($sql, 'warehouse');
            }
            of::work(true);
            return true;
        } catch (Exception $exp) {
            of::work($exp);
            return false;
        }
    }

    //获取此仓库默认库位
    private static function position($warehouse_id)
    {
        $sql = "SELECT position_id, area_id FROM `position` WHERE 1 AND warehouse_id = '{$warehouse_id}';";
        $rows = L::sql($sql, 'warehouse');
        return (!empty($rows)) ? $rows[0] : [];
    }
}